package com.taoge.electricalAppSystem.service;

import com.taoge.electricalAppSystem.dao.DateProduct;
import com.taoge.electricalAppSystem.domain.Product;
import com.taoge.electricalAppSystem.utils.BaseDAO;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

public class Mallservicesimpl implements MallServices {
    public static final Scanner INPUT = new Scanner(System.in);

    @Override
    public void buy() {
        // 更新数据
        DateProduct.update();
        System.out.println("商品编号\t\t商品名称\t\t商品价格\t\t商品库存");
        for (int i = 0; i < DateProduct.proList.size(); i++) {
            System.out.println(DateProduct.proList.get(i));
        }
        inner:
        while (true) {
        System.out.println("请输入商品编号");
        String choice = INPUT.next();
        for (int i = 0; i < DateProduct.proList.size(); i++) {
            if(choice.equals(DateProduct.proList.get(i).getId())){
                    System.out.println("请输入购买数量");
                    Integer num = INPUT.nextInt();
                    if(num < 0){
                        System.out.println("购买数量不能为负数");
                        continue inner;
                    }else{
                        if(num > DateProduct.proList.get(i).getStock()){
                            System.out.println("数量不能超过" +  DateProduct.proList.get(i).getStock());
                            continue inner;
                        }else{
                            System.out.println("您还要继续购买吗");
                            String judge = INPUT.next();
                            if("y".equals(judge)){
                                Product product = new Product(DateProduct.proList.get(i).getId() ,DateProduct.proList.get(i).getName() , DateProduct.proList.get(i).getPrice() , num);
                                // 修改链表中的库存
                                //DateProduct.proList.get(i).setStock(DateProduct.proList.get(i).getStock() - num);
                                // 若重复叠加库存
                                for (int  j = 0; j < DateProduct.carList.size(); j++) {
                                    if(choice.equals(DateProduct.carList.get(j).getId())){
                                        DateProduct.carList.get(i).setStock(DateProduct.carList.get(j).getStock() + num);
                                        continue inner;
                                    }
                                }
                                DateProduct.carList.add(product);
                                continue inner;
                            }else{
                                Product product = new Product(DateProduct.proList.get(i).getId() ,DateProduct.proList.get(i).getName() , DateProduct.proList.get(i).getPrice() , num);
                                // DateProduct.proList.get(i).setStock(DateProduct.proList.get(i).getStock() - num);
                                // 若重复叠加库存
                                for (int  j = 0; j < DateProduct.carList.size(); j++) {
                                    if(choice.equals(DateProduct.carList.get(j).getId())){
                                        DateProduct.carList.get(i).setStock(DateProduct.carList.get(j).getStock() + num);
                                        break inner;
                                    }
                                }
                                // 添加商品到购物车
                                DateProduct.carList.add(product);
                                break inner;
                            }
                        }
                    }
                }
            }
            System.out.println("商品编号不存在");
            break;
        }
        // 循环判断每个数据是否在数据库中存在
        for (int i = 0; i < DateProduct.carList.size(); i++) {
            // 调用BaseDAO.inspectDate函数判断  不存在返回0
            if((BaseDAO.inspectDate("select count(*) from carlist where id = ? "  , DateProduct.carList.get(i).getId())) > 0){
                // 如果存在修改数据库中的库存
                BaseDAO.updateOneRow("update carlist set stock = ? where id = ?" , DateProduct.carList.get(i).getStock() , DateProduct.carList.get(i).getId());
            }else{
                // 不在插入新的数据到数据库中
                BaseDAO.insertOneRow("insert into carlist value(?,?,?,?)" , DateProduct.carList.get(i).getId() ,  DateProduct.carList.get(i).getName() ,  DateProduct.carList.get(i).getPrice() ,  DateProduct.carList.get(i).getStock());
            }
        }

    }



    @Override
    public void shoppingCart() {
        DateProduct.carList = BaseDAO.selectMutiply("select * from carlist", Product.class);
        BigDecimal money = new BigDecimal(0.0);
        System.out.println("商品编号\t\t商品名称\t\t商品价格\t\t购买数量");
        for (int i = 0; i < DateProduct.carList.size(); i++) {
            System.out.println(DateProduct.carList.get(i));
            money = money.add(DateProduct.carList.get(i).getPrice().multiply(new BigDecimal(DateProduct.carList.get(i).getStock())));
        }
        System.out.println("总金额为:");
        System.out.println(money);
        System.out.println("是否购买y");
        String choice = INPUT.next();
        while ("y".equals(choice)){
            System.out.println("输入付款金额");
            BigDecimal Umoney= new BigDecimal(INPUT.nextDouble());
            if(Umoney.compareTo(money) < 0){
                System.out.println("金额不足");
            }else{
                System.out.println("找零" + (Umoney.subtract(money)).setScale(2 , RoundingMode.HALF_UP));
                // 清空购物车
                for (int i = 0; i < DateProduct.carList.size(); i++) {
                    System.out.println(DateProduct.carList.get(i));
                    money = money.add(DateProduct.carList.get(i).getPrice().multiply(new BigDecimal(DateProduct.carList.get(i).getStock())));
                    // 修改数据库中的库存
                    BaseDAO.updateOneRow("update product set stock = ? where id = ?" , DateProduct.proList.get(i).getStock() - DateProduct.carList.get(i).getStock() , DateProduct.proList.get(i).getId());
                    // 删除购物车的数据
                    BaseDAO.deleteOneRow("delete from carlist where id = ?" , DateProduct.carList.get(i).getId());
                }
                DateProduct.carList.clear();
                break;
            }
        }

    }

    @Override
    public void personInformation() {

    }
}
